%%% Logistic Growth Master Script %%%
%%% master.m %%%
warning off all;
% ---------------------------------------------------------------------

b=log(2)./(doub); %specific growth rate (mu)
rates = struct('k',k,'b',b,'er',er,'ec',ec,'d',d,'delta',delta,'Fsr',Fsr,'Fsc',Fsc,'Frc',Frc,'gr',gr,'gc',gc,'ta',ta);
[t pop]=ode15s(@logistic_t,[0 tf],pop0,odeset('reltol',1e-5),rates);
plot(t,pop(:,1),'b-',t,pop(:,2),'k-.',t,pop(:,3),'r.')
Blue is S, Black is R, Red is C
xlabel('t');
ylabel('N(t) (cfu)');
title('Logistic growth');
legend('S','R','C');


%Select stable outcomes%
disp('Selecting for stable outcomes')

stable = cell(0,4);
unstable = cell(0,4);
for x = 1:size(Data(:,1))
   curr = Data(x,:);
       % Check that first derivative of all three functions are at least
       % 95% similar
       dPdt = logistic_t(curr{2}(end),curr{3}(end,1:3),curr{1});
       margin = abs(.05 * dPdt(1))+.0001;
       if (abs(dPdt(1) - dPdt(2)) <= margin || abs(dPdt(3) - dPdt(2)) <= margin || abs(dPdt(1) - dPdt(3)) <= margin || (curr{3}(end,1) < 1 && curr{3}(end,2) <1) || (curr{3}(end,3) < 1 && curr{3}(end,2) <1) || (curr{3}(end,1) < 1 && curr{3}(end,3) <1)) && curr{2}(end) == 32
           stable(end+1,:) = [x curr];
           continue;
       end
       unstable(end+1,:) = [x curr];
end
save 'DataFinal_StableSorted.mat' 'stable' 'unstable';
clear 'Data';

%Pick Winners%
disp('Picking winners.');

S = cell(0,4);
R = cell(0,4);
C = cell(0,4);
Dead = cell(0,4);
Fair = cell(0,4);
for x = 1:size(stable(:,1))
   curr = stable(x,:);
   % Check which population had an absolute advantage at the final data
   % point
   margin = mean(curr{4}(end,:)) *.05;
   if abs(curr{4}(end,1) - curr{4}(end,2)) < margin || abs(curr{4}(end,1) - curr{4}(end,3)) < margin || abs(curr{4}(end,2) - curr{4}(end,3)) < margin
       Fair(end+1,:) = curr;
   elseif curr{4}(end,1) > curr{4}(end,2) && curr{4}(end,1) > curr{4}(end,3) && curr{4}(end,1) > 1.0
       S(end+1,:) = curr;
   elseif curr{4}(end,2) > curr{4}(end,1) && curr{4}(end,2) > curr{4}(end,3) && curr{4}(end,2) > 1.0
       R(end+1,:) = curr;
   elseif curr{4}(end,3) > curr{4}(end,1) && curr{4}(end,3) > curr{4}(end,2) && curr{4}(end,3) > 1.0
       C(end+1,:) = curr;
   else
       Dead(end+1,:) = curr;
   end
end
save 'DataFinal_WinnerSorted_Stable.mat' 'S' 'R' 'C' 'Dead' 'Fair';
clear 'S' 'R' 'C' 'Dead' 'Fair';


S = cell(0,4);
R = cell(0,4);
C = cell(0,4);
Dead = cell(0,4);
Fair = cell(0,4);
for x = 1:size(unstable(:,1))
   curr = unstable(x,:);
   % Check which population had an absolute advantage at the final data
   % point
   margin = mean(curr{4}(end,:)) *.05;
   if abs(curr{4}(end,1) - curr{4}(end,2)) < margin || abs(curr{4}(end,1) - curr{4}(end,3)) < margin || abs(curr{4}(end,2) - curr{4}(end,3)) < margin
       Fair(end+1,:) = curr;
   elseif curr{4}(end,1) > curr{4}(end,2) && curr{4}(end,1) > curr{4}(end,3) && curr{4}(end,1) > 1.0
       S(end+1,:) = curr;
   elseif curr{4}(end,2) > curr{4}(end,1) && curr{4}(end,2) > curr{4}(end,3) && curr{4}(end,2) > 1.0
       R(end+1,:) = curr;
   elseif curr{4}(end,3) > curr{4}(end,1) && curr{4}(end,3) > curr{4}(end,2) && curr{4}(end,3) > 1.0
       C(end+1,:) = curr;
   else
       Dead(end+1,:) = curr;
   end
end
save 'DataFinal_WinnerSorted_UnStable.mat' 'S' 'R' 'C' 'Dead' 'Fair';
clear;
